/*
 * $Id: SimpleTimer.java 30 2006-06-08 13:26:40Z wjx $
 */
package com.someok.utils.datetime;

/**
 * Measures time periods.
 *
 * <p><a href="SimpleTimer.java.html"><i>View Source</i></a></p>
 *
 * @version $Revision: 30 $ $Date: 2006-06-08 21:26:40 +0800 (星期四, 08 六月 2006) $
 */
public class SimpleTimer {

	private long startTime;
	private long stopTime = 0;


	/**
	 * Initialization and timer begins to count.
	 */
	public SimpleTimer() {
		stopTime = 0;
		start();
	}

	/**
	 * Starts the timer. If timer was already running, it resets.
	 */
	public void start() {
		startTime = System.currentTimeMillis();
	}

	/**
	 * Stops timer and returns elapsed time from last timer start in ms.
	 *
	 * @return elapsed time in ms
	 */
	public long stop() {
		stopTime = System.currentTimeMillis();
		return stopTime - startTime;
	}

	/**
	 * Returns elapsed time from the last start and keeps timer running.
	 *
	 * @return elapsed time in ms
	 */
	public long lap() {
		return System.currentTimeMillis() - startTime;
	}

	/**
	 * Stops timer if running. Returns elapsed time from the last timer start.
	 * If timer is not running, returns last measured time.
	 *
	 * @return elapsed time in ms
	 */
	public long elapsed() {
		if (stopTime < startTime) {
			stopTime = System.currentTimeMillis();
		}
		return stopTime - startTime;
	}


	/**
	 * Returns elapsed time as String from the last start.
	 *
	 * @return elapsed time in ms
	 */
	public String toString() {
		return (new Long(stopTime - startTime)).toString();
	}
}
